package com.cloud.controller;


import com.cloud.convert.CloudLogRecordConvertor;
import com.cloud.entity.CloudLogRecord;
import com.cloud.entity.HbaseEntity;
import com.cloud.utils.HBaseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@Api(tags = "测试接口")
public class HbaseController {
    @Autowired
    private HBaseUtils hbaseUtils;
    @Autowired
    private CloudLogRecordConvertor cloudLogRecordConvertor;

    @ApiOperation(value = "Hbase插入表测试-单条")
    @GetMapping(value = "insertOneTest")
    public void insertOneTest(@RequestParam("tableName") String tableName) throws Exception {
        List<Pair<String, String>> pairs1 = Arrays.asList(new Pair<>("name", "Tom1"),
                new Pair<>("age", "25"),
                new Pair<>("gender", "1"));
        boolean result = hbaseUtils.putRow(tableName, "logs", pairs1);
    }

    @ApiOperation(value = "Hbase插入表测试-多条")
    @GetMapping(value = "insertBatchTest")
    public void insertBatchTest(@RequestParam("tableName") String tableName) throws Exception {
        List listRecord = new ArrayList();
        for (int i = 0; i < 1000; i++) {
            CloudLogRecord record = new CloudLogRecord();
            record.setId(i + "");
            record.setReqUser("app");
            record.setAcctType("user");
            record.setSourceApplicationName("DemoA");
            record.setTargetApplicationName("DemoB");
            record.setReqTime(new Date());
            record.setReqMethod("POST");
            record.setReqParam("{'a'='1'}");
            record.setReqAddr("/show");
            record.setTraceId("" + i);
            record.setSkyTraceId("" + i);
            listRecord.add(record);
        }

        for (int i = 0; i < 10; i++) {
            CloudLogRecord record = new CloudLogRecord();
            record.setId(i + "");
            record.setReqUser("app");
            record.setAcctType("user");
            record.setSourceApplicationName("DemoB");
            record.setTargetApplicationName("DemoA");
            record.setReqTime(new Date());
            record.setReqMethod("POST");
            record.setReqParam("{'a'='1'}");
            record.setReqAddr("/show");
            record.setTraceId("" + i);
            record.setSkyTraceId("" + i);
            listRecord.add(record);
        }
        List<HbaseEntity> hbaseEntities = cloudLogRecordConvertor.convertList(listRecord);
        hbaseUtils.insertDataList(tableName, "logs", hbaseEntities);
    }

    @GetMapping(value = "batchInsert")
    public void test() throws Exception {
        List<CloudLogRecord> listRecord = new ArrayList();
        for (int i = 0; i < 10; i++) {
            CloudLogRecord record = new CloudLogRecord();
            record.setId(i + "");
            record.setReqUser("app");
            record.setAcctType("user");
            record.setSourceApplicationName("DemoA");
            record.setTargetApplicationName("DemoB");
            record.setReqTime(new Date());
            record.setReqMethod("POST");
            record.setReqParam("{'a'='1'}");
            record.setReqAddr("/show");
            record.setTraceId("" + i);
            record.setSkyTraceId("" + i);
            listRecord.add(record);
        }

        for (int i = 0; i < 10000; i++) {
            log.info("for循环线程=" + Thread.currentThread().getName());
            List<HbaseEntity> hbaseEntities = cloudLogRecordConvertor.convertList(listRecord);
            hbaseUtils.insertDataListSync("cloud_log_record_202205", "logs", hbaseEntities);
            log.info("循环次数===" + i);
        }
    }
}
